import LoginInterface from "./LoginInterface";
import PayInterface from "./PayInterface";
import SDKLockState from "./SDKLockState";
import VideoAdInterface from "./VideoAdInterface";
import BannerAdInterface from "./BannerAdInterface";
import ShareInterface from "./ShareInterface";
import ExchangeVolume from "./ExchangeVolume";
import ToastController from "../../game/public/toast/ToastController";
import GameBoxItemModel from "../../game/logic/gamebox/GameBoxItemModel";
import InterstitialAd from "./InterstitialAd";
import { isNull } from "../../framework/tools/Define";



export default  class BaseChannel {
    protected id:number;

    protected sdk;

    protected loginMgr:LoginInterface;

    protected payMgr:PayInterface;

    protected rewardAd: VideoAdInterface;

    protected lockState: SDKLockState;

    protected bannerAd: BannerAdInterface;

    protected share:ShareInterface;

    protected exchagneVolume:ExchangeVolume;

    protected interstitialAd: InterstitialAd;

    constructor(id) {
        this.id = id;
        this.lockState = new SDKLockState();
    }

    getID():number{
        return this.id;
    }

    getRewardAd():VideoAdInterface{
        return this.rewardAd;
    }

    getBannerAd():BannerAdInterface{
        return this.bannerAd;
    }

    getShare(){
        return this.share;
    }

    getPay(){
        return this.payMgr;
    }

    getLoginMgr(): LoginInterface{
        return this.loginMgr;
    }


    getLockState(){
        return this.lockState;
    }


    isHaveBannerAd(){
        return this.bannerAd != undefined;
    }



    isHaveStore(){
        return this.payMgr != undefined;
    }

    /**
     * 
     */
    isHaveShare(){
        return this.share != undefined;
    }

    // 初始化结束
    initFinish(){
        
    }

    //初始化sdk
    init() {
        // cc.log(" ",this.data);
   
    }


    //添加sdk代码
    addScript(url: string) {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.id = ""+this.id;
        script.onload =this.initFinish.bind(this);
        script.src = url;
        script.onerror = function() {
            // script.src = url;
            cc.error(" load script error ", url);
        }        
        head.appendChild(script);        
    }



    /**
     * 显示商城 如果没有不重写
     */
    showStore(){

    }
    /**
     * 免费复活
     */
    freeRevive(){

    }

    login(){
        this.loginMgr.login()
    }

    logout(){
        this.loginMgr.logout();
    }

    showBanner(){
        if(this.bannerAd ){
            this.bannerAd.show();
        }
    }

    hideBanner(){
        if(this.bannerAd ){
            this.bannerAd.hide();
        }
    }

    showShare(){

    }

    hideShare(){

    }

    getExchangeVolume(){
        return this.exchagneVolume;
    }


    openRewardAd(func:Function):void{
        if(this.isHaveRewardAd()){
            this.rewardAd.show((isFinish)=>{
                if(isFinish){
                    func(true)
                }else{
                    func(false)
                    ToastController.instance().intoLayer('ui.not_finish');
                }
            })
        }else{
            func(true)
        }
    }

    isHaveRewardAd():boolean{
        return this.rewardAd && this.rewardAd.isLoad();
    }

    isHaveExchangeVolume(ignore:boolean = false){
        if(!ignore){
            return this.exchagneVolume && cc.sys.os != cc.sys.OS_IOS;
        }else{
            return !isNull(this.exchagneVolume) ;
        }
        
    }

    exchangeVolumeInit(func:(isShow:boolean)=>void){
        if(this.isHaveExchangeVolume()){
            this.exchagneVolume.init()
        }else{
            func(false)
        }
    }


    showImage(imageUrl:string){

    }

    jumpToApp(appID:string){

    }

    getAds(num: number, func: (result: any) => void) {
        if(this.exchagneVolume){
            this.exchagneVolume.getAds(num,(list:GameBoxItemModel[])=>{
                if(list.length > 0){
                    func(list)
                }
            })
        }
    }


    isHaveInterstitalAd(){
        return this.interstitialAd && this.interstitialAd.isLoad();
    }

    showInterstitialAd(func:(code)=>void){
        if(this.isHaveInterstitalAd()){
            this.interstitialAd.show(func)
        }
    }

    intoLobby(){

    }
}
